package org.lora.biz.ou.service.impl;

import java.util.List;
import java.util.Map;

import org.lora.biz.ou.LoraOUConstant;
import org.lora.biz.ou.entity.Employee;
import org.lora.biz.ou.entity.EmployeeExample;
import org.lora.biz.ou.entity.EmployeeIdentity;
import org.lora.biz.ou.entity.EmployeeIdentityExample;
import org.lora.biz.ou.entity.EmployeeIdentityView;
import org.lora.biz.ou.entity.EmployeeIdentityViewExample;
import org.lora.biz.ou.entity.Position;
import org.lora.biz.ou.entity.PositionExample;
import org.lora.biz.ou.exception.LoraOrgUserException;
import org.lora.biz.ou.mapper.EmployeeIdentityMapper;
import org.lora.biz.ou.mapper.EmployeeIdentityViewMapper;
import org.lora.biz.ou.mapper.EmployeeMapper;
import org.lora.biz.ou.mapper.PositionMapper;
import org.lora.biz.ou.service.IEmployeeIdentityService;
import org.lora.core.annotation.AutoAssemble;
import org.lora.core.annotation.Service;
import org.lora.core.annotation.Transaction;
import org.lora.core.mybatis.OrderByBean;
import org.lora.core.mybatis.Page;
import org.lora.util.StringUtil;
import org.lora.util.ValidateUtil;
import org.lora.webframework.common.FrameWorkBaseService;

import sun.swing.StringUIClientPropertyKey;

/**
 * <p>
 * 类名:EmployeeIdentityServiceImpl
 * </p>
 * 描述:<br>
 * 创建时间:2016年8月30日 上午10:29:12<br>
 * 
 * @author Bladnir@outlook.com<br>
 *         修改历史:<br>
 *         2016年8月30日 Bladnir 创建<br>
 */
@Service
public class EmployeeIdentityServiceImpl extends FrameWorkBaseService implements IEmployeeIdentityService {

	@AutoAssemble
	private EmployeeMapper employeeMapper;

	@AutoAssemble
	private PositionMapper positionMapper;

	@AutoAssemble
	private EmployeeIdentityViewMapper employeeIdentityViewMapper;

	@AutoAssemble
	private EmployeeIdentityMapper employeeIdentityMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.ou.service.IEmployeeIdentityService#getEmployeeList(java
	 * .lang.String, java.util.Map, org.lora.core.mybatis.Page, java.util.List)
	 */
	@Override
	public List<Employee> getEmployeeList(String domainId, Map<String, Object> parameterMap, Page page, List<OrderByBean> orderByList)
			throws LoraOrgUserException {
		EmployeeExample example = new EmployeeExample();
		org.lora.biz.ou.entity.EmployeeExample.Criteria criteria = example.createCriteria();

		setParameterForEmployee(domainId, parameterMap, criteria);

		// 排序
		String orderByClause = getOrderByClause(orderByList);
		if (StringUtil.isNotNull(orderByClause)) {
			example.setOrderByClause(orderByClause);
		}

		return employeeMapper.selectByExample(example, page);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.ou.service.IEmployeeIdentityService#getEmployeeCount(java
	 * .lang.String, java.util.Map)
	 */
	@Override
	public int getEmployeeCount(String domainId, Map<String, Object> parameterMap) throws LoraOrgUserException {
		EmployeeExample example = new EmployeeExample();
		org.lora.biz.ou.entity.EmployeeExample.Criteria criteria = example.createCriteria();
		setParameterForEmployee(domainId, parameterMap, criteria);

		return employeeMapper.countByExample(example);
	}

	/**
	 * 查询Employee表设置参数
	 * 
	 * @param domainId
	 * @param parameterMap
	 * @param criteria
	 */
	private void setParameterForEmployee(String domainId, Map<String, Object> parameterMap,
			org.lora.biz.ou.entity.EmployeeExample.Criteria criteria) {

		if (StringUtil.isNotNull(domainId)) {
			criteria.andDomainIdEqualTo(domainId);
		}

		String employeeName = getStringFromMap("employeeName", parameterMap);
		if (StringUtil.isNotNull(employeeName)) {
			criteria.andNameLike(LoraOUConstant.PERCENT + employeeName + LoraOUConstant.PERCENT);
		}

		String serialNumber = getStringFromMap("serialNumber", parameterMap);
		if (StringUtil.isNotNull(serialNumber)) {
			criteria.andSerialNumberLike(LoraOUConstant.PERCENT + serialNumber + LoraOUConstant.PERCENT);
		}

		String accountName = getStringFromMap("accountName", parameterMap);
		if (StringUtil.isNotNull(accountName)) {
			criteria.andAccountNameLike(LoraOUConstant.PERCENT + accountName + LoraOUConstant.PERCENT);
		}

		String cellphoneNumber = getStringFromMap("cellphoneNumber", parameterMap);
		if (StringUtil.isNotNull(cellphoneNumber)) {
			criteria.andCellphoneNumberLike(LoraOUConstant.PERCENT + cellphoneNumber + LoraOUConstant.PERCENT);
		}

		String state = getStringFromMap("state", parameterMap);
		if (StringUtil.isNotNull(state)) {
			criteria.andStateEqualTo(state);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.ou.service.IEmployeeIdentityService#getPositionList(java
	 * .lang.String, java.util.Map, org.lora.core.mybatis.Page, java.util.List)
	 */
	@Override
	public List<Position> getPositionList(String domainId, Map<String, Object> parameterMap, Page page, List<OrderByBean> orderByList)
			throws LoraOrgUserException {
		PositionExample example = new PositionExample();
		org.lora.biz.ou.entity.PositionExample.Criteria criteria = example.createCriteria();

		// 排序
		String orderByClause = getOrderByClause(orderByList);
		if (StringUtil.isNotNull(orderByClause)) {
			example.setOrderByClause(orderByClause);
		}
		setParameterForPosition(domainId, parameterMap, criteria);

		return positionMapper.selectByExample(example, page);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.ou.service.IEmployeeIdentityService#getPositionCount(java
	 * .lang.String, java.util.Map)
	 */
	@Override
	public int getPositionCount(String domainId, Map<String, Object> parameterMap) throws LoraOrgUserException {

		PositionExample example = new PositionExample();
		org.lora.biz.ou.entity.PositionExample.Criteria criteria = example.createCriteria();
		setParameterForPosition(domainId, parameterMap, criteria);
		return positionMapper.countByExample(example);
	}

	/**
	 * 设置查询参数
	 * 
	 * @param domainId
	 * @param parameterMap
	 * @param criteria
	 */
	private void setParameterForPosition(String domainId, Map<String, Object> parameterMap,
			org.lora.biz.ou.entity.PositionExample.Criteria criteria) {

		if (StringUtil.isNotNull(domainId)) {
			criteria.andDomainIdEqualTo(domainId);
		}

		String positionName = getStringFromMap("positionName", parameterMap);
		if (StringUtil.isNotNull(positionName)) {
			criteria.andPositionNameLike(LoraOUConstant.PERCENT + positionName + LoraOUConstant.PERCENT);
		}

		String positionLevel = getStringFromMap("positionLevel", parameterMap);
		if (StringUtil.isNotNull(positionLevel)) {
			criteria.andPositionLevelEqualTo(Integer.parseInt(positionLevel));
		}

		String state = getStringFromMap("state", parameterMap);
		if (StringUtil.isNotNull(state)) {
			criteria.andStateEqualTo(state);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.ou.service.IEmployeeIdentityService#saveEmployee(org.lora
	 * .biz.ou.entity.Employee)
	 */
	@Override
	public int saveEmployee(Employee employee) throws LoraOrgUserException {

		int result;

		String id = employee.getId();
		if (StringUtil.isNull(id)) {
			employee.setId(creatBeanId(LoraOUConstant.EMPLOYEE_PREFIX));
			result = employeeMapper.insert(employee);
		} else {
			EmployeeExample example = new EmployeeExample();
			example.createCriteria().andIdEqualTo(id);
			result = employeeMapper.updateByExample(employee, example);
		}

		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.lora.biz.ou.service.IEmployeeIdentityService#
	 * getEmployeeIdentityDetailCount(java.lang.String, java.util.Map)
	 */
	@Override
	public int getEmployeeIdentityDetailCount(String domainId, Map<String, Object> parameterMap) throws LoraOrgUserException {
		EmployeeIdentityViewExample example = new EmployeeIdentityViewExample();

		org.lora.biz.ou.entity.EmployeeIdentityViewExample.Criteria criteria = example.createCriteria();

		setParameterForEmployeeIdentityDetail(domainId, criteria, parameterMap);

		return employeeIdentityViewMapper.countByExample(example);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.lora.biz.ou.service.IEmployeeIdentityService#
	 * getEmployeeIdentityDetailList(java.lang.String, java.util.Map,
	 * org.lora.core.mybatis.Page, java.util.List)
	 */
	@Override
	public List<EmployeeIdentityView> getEmployeeIdentityDetailList(String domainId, Map<String, Object> parameterMap, Page page,
			List<OrderByBean> orderByList) throws LoraOrgUserException {

		EmployeeIdentityViewExample example = new EmployeeIdentityViewExample();

		org.lora.biz.ou.entity.EmployeeIdentityViewExample.Criteria criteria = example.createCriteria();

		setParameterForEmployeeIdentityDetail(domainId, criteria, parameterMap);

		// 排序
		String orderByClause = getOrderByClause(orderByList);
		if (StringUtil.isNotNull(orderByClause)) {
			example.setOrderByClause(orderByClause);
		}

		return employeeIdentityViewMapper.selectByExample(example, page);
	}

	/**
	 * @param criteria
	 * @param parameterMap
	 */
	private void setParameterForEmployeeIdentityDetail(String domainId,
			org.lora.biz.ou.entity.EmployeeIdentityViewExample.Criteria criteria, Map<String, Object> parameterMap) {

		if (StringUtil.isNotNull(domainId)) {
			criteria.andDomainIdEqualTo(domainId);
		}

		String orgId = getStringFromMap("orgId", parameterMap);
		if (StringUtil.isNotNull(orgId)) {
			criteria.andOrgIdEqualTo(orgId);
		}
		
		String eiId = getStringFromMap("eiId", parameterMap);
		if (StringUtil.isNotNull(eiId)) {
			criteria.andEiIdEqualTo(eiId);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.ou.service.IEmployeeIdentityService#saveEmployeeIdentity
	 * (org.lora.biz.ou.entity.EmployeeIdentity)
	 */
	@Override
	public int saveEmployeeIdentity(EmployeeIdentity employeeIdentity) throws LoraOrgUserException {

		String id = employeeIdentity.getId();

		if (StringUtil.isNull(id)) {
			employeeIdentity.setId(super.creatBeanId(LoraOUConstant.EMPLOYEE_IDENTITY_PREFIX));
			return employeeIdentityMapper.insert(employeeIdentity);
		} else {
			EmployeeIdentityExample example = new EmployeeIdentityExample();
			example.createCriteria().andIdEqualTo(id);

			return employeeIdentityMapper.updateByExample(employeeIdentity, example);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.lora.biz.ou.service.IEmployeeIdentityService#
	 * getEmployeeIdentityDetailForComplete(java.lang.String,
	 * org.lora.core.mybatis.Page, java.lang.String, java.util.List)
	 */
	@Override
	public List<EmployeeIdentityView> getEmployeeIdentityDetailForComplete(String domainId, Page page, String keyword,
			List<OrderByBean> orderByList) throws LoraOrgUserException {

		EmployeeIdentityViewExample example = new EmployeeIdentityViewExample();

		if (StringUtil.isNull(keyword)) {
			org.lora.biz.ou.entity.EmployeeIdentityViewExample.Criteria criteria0 = example.createCriteria();
			criteria0.andEiStateEqualTo(LoraOUConstant.STATE_ACTIVATE);
		} else {

			org.lora.biz.ou.entity.EmployeeIdentityViewExample.Criteria criteria0 = example.or();
			org.lora.biz.ou.entity.EmployeeIdentityViewExample.Criteria criteria1 = example.or();
			org.lora.biz.ou.entity.EmployeeIdentityViewExample.Criteria criteria2 = example.or();

			criteria0.andAccountNameLike(LoraOUConstant.PERCENT + keyword + LoraOUConstant.PERCENT);
			criteria0.andEiStateEqualTo(LoraOUConstant.STATE_ACTIVATE);

			criteria1.andEmpNameLike(LoraOUConstant.PERCENT + keyword + LoraOUConstant.PERCENT);
			criteria1.andEiStateEqualTo(LoraOUConstant.STATE_ACTIVATE);

			criteria2.andEmpSerialNumberLike(LoraOUConstant.PERCENT + keyword + LoraOUConstant.PERCENT);
			criteria2.andEiStateEqualTo(LoraOUConstant.STATE_ACTIVATE);
		}

		return employeeIdentityViewMapper.selectByExample(example, page);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.ou.service.IEmployeeIdentityService#getEmployeeIdentityById
	 * (java.lang.String, java.lang.String)
	 */
	@Override
	public EmployeeIdentity getEmployeeIdentityById(String domainId, String eiId) throws LoraOrgUserException {

		if (StringUtil.isNull(domainId)) {
			throw new LoraOrgUserException("domainId is null");
		}

		if (StringUtil.isNull(eiId)) {
			throw new LoraOrgUserException("eiId is null");
		}

		EmployeeIdentityExample example = new EmployeeIdentityExample();
		org.lora.biz.ou.entity.EmployeeIdentityExample.Criteria criteria = example.createCriteria();
		criteria.andDomainIdEqualTo(domainId);
		criteria.andIdEqualTo(eiId);

		List<EmployeeIdentity> list = employeeIdentityMapper.selectByExample(example);
		if (ValidateUtil.isNotEmpty(list)) {
			return list.get(0);
		}

		return null;
	}

}
